package org.mineground.handlers;

import java.sql.Connection;
import java.sql.DriverManager;

import org.mineground.Main;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @file MySQLHandler.java (22.01.2012)
 * @author Daniel Koenen
 *
 */
public class MySQLHandler {
    private static final Logger ExceptionLogger = LoggerFactory.getLogger(MySQLHandler.class);
    
    private Connection mySQLDatabase;
    
    public MySQLHandler() {
        ConfigHandler configHandler = Main.getInstance().getConfigHandler();
        String databaseURL = "jdbc:mysql://" + configHandler.getMySQLHost() + ":" + configHandler.getMySQLPort() + "/" + configHandler.getMySQLDatabase() + "?autoReconnect=true&useUnicode=true&characterEncoding=utf8";
        
        try {
            Class.forName("com.mysql.jdbc.Driver");
            mySQLDatabase = DriverManager.getConnection(databaseURL, configHandler.getMySQLUser(), configHandler.getMySQLPassword());
        }
        
        catch (Exception exception) {
            ExceptionLogger.error("Exception caught", exception);
        }
    }
    
    public Connection getConnection() {
        return mySQLDatabase;
    }
}
